/**
 * Date:2021年4月23日 上午10:50:09
 * Copyright (c) 2021, www.bwbroad.com All Rights Reserved
 */

package com;

import java.io.IOException;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import com.xue.antlr.clickhouse.ClickHouseLexer;
import com.xue.antlr.clickhouse.ClickHouseParser;
import com.xue.antlr.mysql.MySQLLexer;
import com.xue.antlr.mysql.MySQLParser;
import com.xue.impl.sqlBase.SqlBaseTestMain;
import com.xue.impl.sqlBase.parser.SqlParser;
import com.xue.impl.sqlBase.tree.Query;
import com.xue.impl.sqlBase.tree.Statement;

/**
 * Description: TODO <br/>
 * Date:2021年4月23日 上午10:50:09 <br/>
 * 
 * @author xuejianxin
 */
public class SqltestMain {

	public static void main(String[] args) throws IOException {
		//mysql();
		//clickhouse();
		sqlBase();
	}

	private static void mysql() {
		CharStream in = null;
		in = CharStreams.fromString(
				"WITH cte (col1, col2) AS ( SELECT 1, 2 UNION ALL SELECT 3, 4 ) SELECT col1, col2 FROM cte");
		in = CharStreams.fromString("INSERT INTO t2 (id) SELECT id FROM table1  WHERE name = 100");
		// in = CharStreams.fromString("select t.id,t.name from demo t");
		MySQLLexer lexer = new MySQLLexer(in);
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		MySQLParser parser = new MySQLParser(tokens);
		ParseTree tree = parser.query();
		System.out.println(tree.toStringTree(parser));
	}

	private static void clickhouse() {
		CharStream in = CharStreams.fromString("select t.id,t.name from demo t");

		in = CharStreams.fromString("INSERT INTO t2 (id) SELECT t1.id FROM table1  t1 WHERE t1.name = 100");

		// CharStream in = CharStreams.fromStream(System.in);
		ClickHouseLexer lexer = new ClickHouseLexer(in);
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		ClickHouseParser parser = new ClickHouseParser(tokens);
		ParseTree tree = parser.queryStmt();
		System.out.println(tree.toStringTree(parser));
	}

	private static void sqlBase() {

		String inputFile = null;

		String sql = "select City, City from cities";

		SqlParser sqlParser = new SqlParser();

		Statement statement = sqlParser.createStatement(sql);

		if (statement instanceof Query) {
			try {
				SqlBaseTestMain.handleQuery((Query) statement);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else {
			System.out.println("unsupported!!");
		}

	}

}
